package com.hnucm.cooklife.presenter.me;

import androidx.annotation.NonNull;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.Work;
import com.hnucm.cooklife.contract.me.Contract;
import com.hnucm.cooklife.model.MeWorkModel;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MeWorkPresenter extends BasePresenter<Contract.IMeWorkView> implements Contract.IMeWorkPresenter {
    Contract.IMeWorkModel mIMeWorkModel;

    public MeWorkPresenter() {
        mIMeWorkModel = new MeWorkModel();
    }

    @Override
    public void loadMyWork(int userId) {
        mIMeWorkModel.loadMyWork(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Work>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<Work> workList) {
                        if(isViewAttached()){
                            getView().onLoadMyWork(workList);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage());
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshMyWork(int userId) {

    }
}
